'''
Created on 18/08/2010

@author: eh01
'''
from adaptor.services.document_service import DocumentService
from datastore.resource import Resource, ResourceDict
from datastore.spaces import Spaces
from datastore.uri import Uri
import unittest
import adaptor


class DocumentServiceTest(unittest.TestCase):


    def setUp(self):
        pass


    def tearDown(self):
        pass

    def test_cb_start(self):
        spaces = Spaces()
        uri = Uri()
        document_service = DocumentService(spaces=spaces, uri=uri)
        document_service.cb_start(None, None, None)
        self.assertEqual([document_service.cb_get_document], spaces.process[Uri('internal:adaptor/document/')])

    def test_cb_get_document_resposne(self):
        # test it ignores uris that end with resposne
        spaces = Spaces()
        uri = Uri('test/uri/response')
        document_service = DocumentService(spaces=spaces, uri=Uri())
        ret = document_service.cb_get_document(uri, Spaces.ACTION_GET, None)
        self.assertEqual(None, ret)
                
    def test_cb_get_document_get(self):
        spaces = Spaces()
        uri = Uri('test/uri')
        document_service = DocumentService(spaces=spaces, uri=Uri())
        res = document_service.cb_get_document(uri, Spaces.ACTION_GET, None)
        self.assertEqual(None, res)
    
    def test_cb_get_document_put_general(self):
        spaces = Spaces()
        uri = Uri('test/uri')
        spear_reference_number = 'S000001E'
        doc_id = '1'
        res_post = Resource(type='term:base/services/http/post')
        query_data = {}
        query_data['spearRef'] = spear_reference_number
        query_data['docID'] = doc_id
        res_post.query_data = query_data
        document_service = DocumentService(spaces=spaces, uri=Uri())
        
        # test that the app doesn't exist
        response_uri = document_service.cb_get_document(uri, Spaces.ACTION_PUT, res_post)
        self.assertEqual(uri + 'response', response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:eplanning/adaptor/error'), response_rs.get_type())
        self.assertEqual(adaptor.APP_NOT_FOUND_MSG % spear_reference_number, response_rs.error)
        self.assertEqual(adaptor.ERROR_H1_HEADER, response_rs.h1_header)
        
        # test the app exists but no documents yet
        app_res = Resource(type="term:eplanning/spear/application")
        spaces.put(Uri('spear:app/%s' % spear_reference_number), app_res)
        response_uri = document_service.cb_get_document(uri, Spaces.ACTION_PUT, res_post)
        self.assertEqual(uri + 'response', response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:eplanning/adaptor/error'), response_rs.get_type())
        self.assertEqual(DocumentService.DOC_NOT_FOUND_MSG % doc_id, response_rs.error)
        self.assertEqual(adaptor.ERROR_H1_HEADER, response_rs.h1_header)
        
        # test the app exists but the document doesn't exists
        app_res.sid005a_dict_dict = {}
        spaces.put(Uri('spear:app/%s' % spear_reference_number), app_res)
        response_uri = document_service.cb_get_document(uri, Spaces.ACTION_PUT, res_post)
        self.assertEqual(uri + 'response', response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:eplanning/adaptor/error'), response_rs.get_type())
        self.assertEqual(DocumentService.DOC_NOT_FOUND_MSG % doc_id, response_rs.error)
        self.assertEqual(adaptor.ERROR_H1_HEADER, response_rs.h1_header)
        
    def test_cb_get_document_put_accept_reject(self):
        spaces = Spaces()
        uri = Uri('test/uri')
        spear_reference_number = 'S000001E'
        doc_id = '1'
        res_post = Resource(type='term:base/services/http/post')
        query_data = {}
        query_data['spearRef'] = spear_reference_number
        query_data['docID'] = doc_id
        res_post.query_data = query_data
        document_service = DocumentService(spaces=spaces, uri=Uri())
        app_res = Resource(type="term:eplanning/spear/application")
        app_res.sid005a_dict_dict = {}
        
        # test the app exists and the document exists but no attachment
        doc_authenticated_by = 'Somebody'
        doc_submitted_date = '01/01/01'
        doc_suffix = 'Doc Suffix'
        doc_version_number = '1'
        doc_sid005a_dict = {}
        doc_sid005a_dict['sid005a_current_document_doctype'] = adaptor.DOC_TYPE_ACCEPT_REJECT
        doc_sid005a_dict['sid005a_current_document_version_number'] = doc_version_number
        doc_sid005a_dict['sid005a_current_document_outcomesuffixcommand_suffix'] = doc_suffix
        doc_sid005a_dict['sid005a_current_document_submitted_date'] = doc_submitted_date
        doc_sid005a_dict['sid005a_current_document_authenticated_by'] = doc_authenticated_by
        doc_sid004_dict = {}
        doc_sid004_dict['sid004_doc_id'] = doc_id
        doc_sid004_dict['sid004_doctype'] = adaptor.DOC_TYPE_ACCEPT_REJECT
        sid004_dict = {}
        sid004_dict['sid004_attachment_dict_list'] = [doc_sid004_dict]
        app_res.sid004_dict = sid004_dict
        app_res.sid005a_dict_dict[doc_id] = doc_sid005a_dict
        spaces.put(Uri('spear:app/%s' % spear_reference_number), app_res)
        response_uri = document_service.cb_get_document(uri, Spaces.ACTION_PUT, res_post)
        self.assertEqual(uri + 'response', response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:eplanning/adaptor/document-accept-reject/data'), response_rs.get_type())
        self.assertEqual(doc_version_number, response_rs.version_number)
        self.assertEqual(doc_suffix, response_rs.outcome)
        self.assertEqual(doc_submitted_date, response_rs.submitted_date)
        self.assertEqual(doc_authenticated_by, response_rs.authenticated_by)
        self.assertEqual(spear_reference_number, response_rs.spear_reference_number)
        self.assertEqual(doc_id, response_rs.doc_id)
        self.assertFalse(response_rs.has_attachment)
        
        # test the app exists, the document exists and it has an attachment but hasn't downloaded yet
        doc_sid005a_dict['sid005a_current_document_size'] = 'Some size'
        spaces.put(Uri('spear:app/%s' % spear_reference_number), app_res)
        response_uri = document_service.cb_get_document(uri, Spaces.ACTION_PUT, res_post)
        self.assertEqual(uri + 'response', response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:eplanning/adaptor/document-accept-reject/data'), response_rs.get_type())
        self.assertEqual(doc_version_number, response_rs.version_number)
        self.assertEqual(doc_suffix, response_rs.outcome)
        self.assertEqual(doc_submitted_date, response_rs.submitted_date)
        self.assertEqual(doc_authenticated_by, response_rs.authenticated_by)
        self.assertEqual(spear_reference_number, response_rs.spear_reference_number)
        self.assertEqual(doc_id, response_rs.doc_id)
        self.assertFalse(response_rs.has_attachment)
                
        # test the app exists, the document exists and has an attachment
        attach_rs = Resource(type='term:eplanning/spear/application-attachment')
        spaces.put(Uri('spear:app-attachment/%s' % doc_id), attach_rs)
        response_uri = document_service.cb_get_document(uri, Spaces.ACTION_PUT, res_post)
        self.assertEqual(uri + 'response', response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:eplanning/adaptor/document-accept-reject/data'), response_rs.get_type())
        self.assertEqual(doc_version_number, response_rs.version_number)
        self.assertEqual(doc_suffix, response_rs.outcome)
        self.assertEqual(doc_submitted_date, response_rs.submitted_date)
        self.assertEqual(doc_authenticated_by, response_rs.authenticated_by)
        self.assertEqual(spear_reference_number, response_rs.spear_reference_number)
        self.assertEqual(doc_id, response_rs.doc_id)
        self.assertTrue(response_rs.has_attachment)
    
    def test_cb_get_document_put_responsible_authority_reference_number(self):
        spaces = Spaces()
        uri = Uri('test/uri')
        spear_reference_number = 'S000001E'
        doc_id = '1'
        res_post = Resource(type='term:base/services/http/post')
        query_data = {}
        query_data['spearRef'] = spear_reference_number
        query_data['docID'] = doc_id
        res_post.query_data = query_data
        document_service = DocumentService(spaces=spaces, uri=Uri())
        app_res = Resource(type="term:eplanning/spear/application")
        app_res.sid005a_dict_dict = {}
        
        # test the app exists and only one reference number
        responsible_authority_reference_number_1 = '123'
        sid072a_dict = {}
        sid072a_dict['sid072a_reference_number_1'] = responsible_authority_reference_number_1
        doc_sid004_dict = {}
        doc_sid004_dict['sid004_doc_id'] = doc_id
        doc_sid004_dict['sid004_doctype'] = adaptor.DOC_TYPE_RESPONSIBLE_AUTHORITY_REF_NUM
        sid004_dict = {}
        sid004_dict['sid004_attachment_dict_list'] = [doc_sid004_dict]
        app_res.sid004_dict = sid004_dict
        app_res.sid072a_dict = sid072a_dict
        spaces.put(Uri('spear:app/%s' % spear_reference_number), app_res)
        response_uri = document_service.cb_get_document(uri, Spaces.ACTION_PUT, res_post)
        self.assertEqual(uri + 'response', response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:eplanning/adaptor/document-council-ref-num/data'), response_rs.get_type())
        self.assertEqual(responsible_authority_reference_number_1, response_rs.council_reference_number_1)
        self.assertEqual(spear_reference_number, response_rs.spear_reference_number)
        self.assertEqual(adaptor.DOC_TYPE_RESPONSIBLE_AUTHORITY_REF_NUM, response_rs.h1_header)
        
        # test the app exists with two reference numbers
        responsible_authority_reference_number_2 = '456'
        sid072a_dict['sid072a_reference_number_2'] = responsible_authority_reference_number_2
        app_res.sid072a_dict = sid072a_dict
        spaces.put(Uri('spear:app/%s' % spear_reference_number), app_res)
        response_uri = document_service.cb_get_document(uri, Spaces.ACTION_PUT, res_post)
        self.assertEqual(uri + 'response', response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:eplanning/adaptor/document-council-ref-num/data'), response_rs.get_type())
        self.assertEqual(responsible_authority_reference_number_1, response_rs.council_reference_number_1)
        self.assertEqual(responsible_authority_reference_number_2, response_rs.council_reference_number_2)
        self.assertEqual(spear_reference_number, response_rs.spear_reference_number)
        self.assertEqual(adaptor.DOC_TYPE_RESPONSIBLE_AUTHORITY_REF_NUM, response_rs.h1_header)        

    def test_cb_get_document_put_referral_request(self):
        spaces = Spaces()
        uri = Uri('test/uri')
        spear_reference_number = 'S000001E'
        doc_id = '1'
        res_post = Resource(type='term:base/services/http/post')
        query_data = {}
        query_data['spearRef'] = spear_reference_number
        query_data['docID'] = doc_id
        res_post.query_data = query_data
        document_service = DocumentService(spaces=spaces, uri=Uri())
        app_res = Resource(type="term:eplanning/spear/application")
        app_res.sid005a_dict_dict = {}
        
        sid061a_dict_a = {}
        sid061a_dict_a['sid061a_ra_requirements'] = 'Section 55'
        sid061a_dict_a['sid061a_council_ra_comments'] = 'Just Some Comments A'
        sid061a_dict_b = {}
        sid061a_dict_b['sid061a_ra_requirements'] = 'Section 55'
        sid061a_dict_b['sid061a_council_ra_comments'] = 'Just Some Comments B'
        sid061a_ra_dict_dict = {}
        sid061a_ra_dict_dict['A'] = sid061a_dict_a
        sid061a_ra_dict_dict['B'] = sid061a_dict_b
        sid061a_dict = {'sid061a_ra_dict_dict': sid061a_ra_dict_dict}
        sid061a_dict_dict = {}
        sid061a_dict_dict[doc_id] = sid061a_dict
        doc_sid004_dict = {}
        doc_sid004_dict['sid004_doc_id'] = doc_id
        doc_sid004_dict['sid004_doctype'] = adaptor.DOC_TYPE_REFERRAL_REQUEST
        sid004_dict = {}
        sid004_dict['sid004_attachment_dict_list'] = [doc_sid004_dict]
        app_res.sid004_dict = sid004_dict
        app_res.sid061a_dict_dict = sid061a_dict_dict
        spaces.put(Uri('spear:app/%s' % spear_reference_number), app_res)
        response_uri = document_service.cb_get_document(uri, Spaces.ACTION_PUT, res_post)
        self.assertEqual(uri + 'response', response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:eplanning/adaptor/document-referral-request/data'), response_rs.get_type())
        self.assertTrue('A' in response_rs.referrals.keys())
        self.assertTrue('B' in response_rs.referrals.keys())
        self.assertEqual(sid061a_dict_a['sid061a_ra_requirements'], response_rs.referrals['A']['referral_requirements'])
        self.assertEqual(sid061a_dict_b['sid061a_ra_requirements'], response_rs.referrals['B']['referral_requirements'])
        self.assertEqual(sid061a_dict_a['sid061a_council_ra_comments'], response_rs.referrals['A']['council_ra_comments'])
        self.assertEqual(sid061a_dict_b['sid061a_council_ra_comments'], response_rs.referrals['B']['council_ra_comments'])
        self.assertEqual(spear_reference_number, response_rs.spear_reference_number)
        self.assertEqual(adaptor.DOC_TYPE_REFERRAL_REQUEST, response_rs.h1_header)  
                
    def test_cb_get_document_exists(self):
        spaces = Spaces()
        uri = Uri('test/uri')
        document_service = DocumentService(spaces=spaces, uri=Uri())
        bool = document_service.cb_get_document(uri, Spaces.ACTION_EXISTS, None)
        self.assertFalse(bool)
        
    def test_cb_get_document_update(self):
        spaces = Spaces()
        uri = Uri()
        document_service = DocumentService(spaces=spaces, uri=uri)
        response_uri = document_service.cb_get_document(Uri('test/uri'), Spaces.ACTION_UPDATE, None)
        self.assertEqual(None, response_uri)
        
    def test_cb_get_document_delete(self):
        spaces = Spaces()
        uri = Uri()
        document_service = DocumentService(spaces=spaces, uri=uri)
        ret = document_service.cb_get_document(Uri('test/uri'), Spaces.ACTION_DELETE, None)
        self.assertEqual(None, ret)

    def test_cb_get_document_remove(self):
        spaces = Spaces()
        uri = Uri()
        document_service = DocumentService(spaces=spaces, uri=uri)
        res = document_service.cb_get_document(Uri('test/uri'), Spaces.ACTION_REMOVE, None)
        self.assertEqual(None, res)

    def test_cb_get_document_find(self):
        spaces = Spaces()
        uri = Uri()
        document_service = DocumentService(spaces=spaces, uri=uri)
        lst = document_service.cb_get_document(Uri('test/uri'), Spaces.ACTION_FIND, None)
        self.assertEqual(None, lst)
             
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()